package com.itlaoqi.rabbit.exchange;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;

public class NewsProducer {

    private RabbitTemplate rabbitTemplate = null;

    public RabbitTemplate getRabbitTemplate() {
        return rabbitTemplate;
    }

    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void sendNews(String routingKey,News news){
        //converAndSend用于exchange发送数据
        //第一个参数是routingKey
        //第一个参数是要传播的对象，可以是字符串，byte[]，或者实现了序列化接口的对象
        rabbitTemplate.convertAndSend(routingKey,news);
        System.out.println("新闻发送成功");
    }

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        NewsProducer np = (NewsProducer)ctx.getBean("newsProducer");
        np.sendNews("us.20190101",new News("新华社","特朗普又退群了",new Date(),"国际新闻内容"));
        np.sendNews("china.20190101",new News("凤凰网","XXX企业荣登500强",new Date(),"国内新闻内容"));

    }
}
